package com.ying.service.order.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.ying.core.exception.CustomException;
import com.ying.core.page.PageData;
import com.ying.core.page.PageWrap;
import com.ying.core.response.ResultCode;
import com.ying.dao.order.WrapRepository;
import com.ying.po.order.Orange;
import com.ying.po.order.Order;
import com.ying.po.order.Wrap;
import com.ying.service.order.WrapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.*;

@Service
public class WrapServiceImpl implements WrapService {

    @Autowired
    private WrapRepository wrapRepository;

    @Override
    public Integer create(Wrap wrap) {
        if (wrap.getId() != null) {
            Optional<Wrap> optional = wrapRepository.findById(wrap.getId());
            if (optional.isPresent()) {
                throw new CustomException(ResultCode.DATA_ALREADY_EXISTED, "WrapServiceImpl.create");
            }
        }
        return wrapRepository.save(wrap).getId();
    }

    @Override
    public void deleteById(Integer id) {
        // 参数检验----------------------------------
        if (id == null)
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "WrapServiceImpl.deleteById");
        Optional<Wrap> optional = wrapRepository.findById(id);
        if (!optional.isPresent()) {
            throw new CustomException(ResultCode.RESULE_DATA_NONE, "WrapServiceImpl.deleteById");
        }
        // ------------开始业务----------------------
        Wrap wrap = optional.get();
        wrap.setIsDelete(1);
        wrapRepository.save(wrap);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        // 参数检验----------------------------------
        if (CollUtil.isEmpty(ids))
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "WrapServiceImpl.deleteByIdInBatch");
        // ------------开始业务----------------------
        for (Integer id: ids) {
            this.deleteById(id);
        }
    }

    @Override
    public Wrap findById(Integer id) {
        // ------------------参数检验---------------
        if (id == null)
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "WrapServiceImpl.findById");
        // ------------开始业务----------------------
        Optional<Wrap> optional = wrapRepository.findByIdAndIsNotDelete(id);
        return optional.orElse(null);
    }

    @Override
    public List<Wrap> findByIdInBatch(List<Integer> ids) {
        // ------------------参数检验---------------
        if (CollUtil.isEmpty(ids))
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "WrapServiceImpl.findByIdInBatch");
        // ------------开始业务----------------------
        List<Wrap> wrapList = new ArrayList<Wrap>();
        for (Integer id : ids) {
            wrapList.add(this.findById(id));
        }
        return wrapList;
    }

    @Override
    public List<Wrap> findOfOneYear(Integer year, Integer userId) {
        List<Wrap> all = wrapRepository.findAll();
        List<Wrap> resultList = new ArrayList<Wrap>();
        if (all.size() == 0) {
            return resultList;
        }
        for (Wrap wrap : all) {
            int i = DateUtil.year(wrap.getDate());
            if (wrap.getIsDelete() == 0 && Objects.equals(userId, wrap.getUserId()) && year.equals(i)) {
                resultList.add(wrap);
            }
        }
        return resultList;
    }

    @Override
    public List<Wrap> findDuringYear(Integer year1, Integer year2, Integer userId) {
        List<Wrap> all = wrapRepository.findAll();
        List<Wrap> resultList = new ArrayList<Wrap>();
        for (Wrap wrap : all) {
            int i = DateUtil.year(wrap.getDate());
            if (wrap.getIsDelete() == 0 && Objects.equals(userId, wrap.getUserId()) && i >= year1 && i <= year2) {
                resultList.add(wrap);
            }
        }
        return resultList;
    }

    @Override
    public List<Map> getWrapPrice(Integer userId) {
        Date date = new Date();
        int thisYear = DateUtil.year(date);
        List<Wrap> wrapList = this.findOfOneYear(thisYear, userId);
        List<BigDecimal> s = new ArrayList<>();
        List<Map> resultList = new ArrayList<>();
        for (Wrap wrap : wrapList) {
            if (!s.contains(wrap.getPrice()) && thisYear == DateUtil.year(wrap.getDate())) {
                s.add(wrap.getPrice());
                Map map = new HashMap();
                map.put("id", wrap.getId());
                map.put("price", wrap.getPrice());
                resultList.add(map);
            }
        }
        return resultList;
    }
}
